package org.dromara.vmp.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.vmp.domain.MonitorArea;
import org.dromara.vmp.domain.bo.MonitorAreaBo;
import org.dromara.vmp.domain.vo.MonitorAreaVo;
import org.dromara.vmp.mapper.MonitorAreaMapper;
import org.dromara.vmp.service.IMonitorAreaService;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 监控区域Service业务层处理
 *
 * @author LSL
 * @date 2024-05-21
 */
@RequiredArgsConstructor
@Service
public class MonitorAreaServiceImpl implements IMonitorAreaService {

    private final MonitorAreaMapper baseMapper;

    /**
     * 查询监控区域
     */
    @Override
    public MonitorAreaVo queryById(Long maId) {
        return baseMapper.selectVoById(maId);
    }

    /**
     * 查询监控区域列表
     */
    @Override
    public TableDataInfo<MonitorAreaVo> queryPageList(MonitorAreaBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<MonitorArea> lqw = buildQueryWrapper(bo);
        Page<MonitorAreaVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询监控区域列表
     */
    @Override
    public List<MonitorAreaVo> queryList(MonitorAreaBo bo) {
        LambdaQueryWrapper<MonitorArea> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<MonitorArea> buildQueryWrapper(MonitorAreaBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<MonitorArea> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getMaName()), MonitorArea::getMaName, bo.getMaName());
        lqw.eq(bo.getCommunityId() != null, MonitorArea::getCommunityId, bo.getCommunityId());
        lqw.like(StringUtils.isNotBlank(bo.getCommunityName()), MonitorArea::getCommunityName, bo.getCommunityName());
        return lqw;
    }

    /**
     * 新增监控区域
     */
    @Override
    public Boolean insertByBo(MonitorAreaBo bo) {
        MonitorArea add = MapstructUtils.convert(bo, MonitorArea.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setMaId(add.getMaId());
        }
        return flag;
    }

    /**
     * 修改监控区域
     */
    @Override
    public Boolean updateByBo(MonitorAreaBo bo) {
        MonitorArea update = MapstructUtils.convert(bo, MonitorArea.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(MonitorArea entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除监控区域
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
